﻿using System.Reflection;
using RedisUtils;
using StackExchange.Redis;


class Program
{
    static int Main(string[] args)
    {
        int iRet = 0;
        string sErr = "";

        string sArg;

        Conf xConf;
        Redis xRedis = new Redis();
        IDatabase xRedisDB;
        SqlSugar.ISqlSugarClient cn;

        try
        {
            for (int i = 1; i < args.Length; i++)
            {
                sArg = args[i].ToLower();

                switch (sArg)
                {
                    case "-json":
                        xRedis.bUseJson = true;
                        break;

                    //case "-text":   
                    //    xRedis.bUseJson = false;
                    //    break;

                    default:
                        xRedis.bUseJson = false;
                        break;
                }
            }

            Console.WriteLine(" ********* INITIALIZATION ****************\n");

            sErr = xRedis.ReadConf(Path.Combine("Config", "redisconf.json"), out xConf);
            if (sErr != "") goto Get_Out;

            // Init DB Conn
            sErr = xRedis.Conn_To_DB(out cn);
            if (sErr != "") goto Get_Out;

            // Init Redis
            ConnectionMultiplexer xRedisConn = ConnectionMultiplexer.Connect(xConf.Redis);
            sErr = SPUtils.RedisUtils.Try_Redis_Conn(xRedisConn, out xRedisDB);
            if (sErr != "") goto Get_Out;

            Console.WriteLine(" Redis: Connected.\n");
            
            Console.WriteLine(" ********* PROCESSING ALIAS-ID ***********\n");

            sErr = xRedis.DB2Redis_AliasID(xRedisDB, cn);
            if (sErr != "") goto Get_Out;

            Console.WriteLine(" ********* PROCESSING PROPERTIES *********\n");

            sErr = xRedis.DB2Redis_Props();
            if (sErr != "") goto Get_Out;

            xRedisConn.Close();
            Console.WriteLine(" ********* PROCESS COMPLETED *************");
        }
        catch (Exception ex)
        {
            sErr = ex.Message + "\r\n" + ex.StackTrace + "\r\nClass: " + MethodBase.GetCurrentMethod()!.DeclaringType
                + "  Method: " + MethodBase.GetCurrentMethod()!.Name;

            iRet = -1;
            goto Get_Out;
        }

    Get_Out:

        if (sErr != "")
        {
            Console.WriteLine(sErr);
            iRet = -1;
        }
        return iRet;
    }
}
